//
//  HRZGoodsService.m
//  EMarket
//
//  Created by 石头 on 2018/7/30.
//  Copyright © 2018 好日子. All rights reserved.
//

#import "HRZGoodsService.h"
// Controllers

// Models
#import "HRZGoodsClassifyBaseModel.h"
#import "HRZGoodsClassifyContentModel.h"
#import "HRZGoodsClassifyDetailModel.h"
#import "HRZGoodsDetailModel.h"
#import "HRZGoodsClassifyListSearchModel.h"
// Views

// Vendors

// Categories

// Others


@interface HRZGoodsService ()

@end

@implementation HRZGoodsService

+ (void)getGoodsBaseClassifySuccess:(void (^)(NSArray<HRZGoodsClassifyBaseModel *> *))success
                            failure:(void (^)(NSString *))failure {
    
    HRZAPIRequest *request = [[HRZAPIRequest alloc] initWithAPIRequestType:kAPIRequestType_Get withSubPath:kFistClassifyUrl];
    
    [HRZAPIClient execute:request];
    
    request.ServerSuce = ^(NSDictionary *sucObj) {
        NSLog(@"%@", sucObj);
        
        [HRZGoodsClassifyBaseModel mj_setupReplacedKeyFromPropertyName:^NSDictionary *{
            return @{
                     @"modelId":@"id"
                     };
        }];
        
        NSArray *array = [HRZGoodsClassifyBaseModel mj_objectArrayWithKeyValuesArray:sucObj];
        
        if (success) {
            success(array);
        }
    };
    
    request.ServerFail = ^(NSString *error) {
        if (failure) {
            failure(error);
        }
    };
}

+ (void)getGoodsClassifyContentByClassifyId:(NSString *)classifyId
                                    success:(void (^)(HRZGoodsClassifyContentModel *))success
                                    failure:(void (^)(NSString *))failure {
    
    NSMutableDictionary *mutDic = [NSMutableDictionary new];
    if (classifyId) [mutDic addEntriesFromDictionary:@{@"classifyId": classifyId}];
    
    HRZAPIRequest *request = [[HRZAPIRequest alloc] initWithAPIRequestType:kAPIRequestType_Get withSubPath:kClassifyContentUrl];
    
    request.params = mutDic;
    
    [HRZAPIClient execute:request];
    
    request.ServerSuce = ^(NSDictionary *sucObj) {
        [HRZGoodsClassifyBaseModel mj_setupReplacedKeyFromPropertyName:^NSDictionary *{
            return @{
                     @"modelId":@"id"
                     };
        }];
        
        HRZGoodsClassifyContentModel *model = [HRZGoodsClassifyContentModel mj_objectWithKeyValues:sucObj];
        if (success) {
            success(model);
        }
    };
    
    request.ServerFail = ^(NSString *error) {
        if (failure) {
            failure(error);
        }
    };
    
}

+ (void)getGoodsListByClassifyModel:(HRZGoodsClassifyListSearchModel *)model
                            success:(void (^)(NSArray<HRZGoodsClassifyDetailModel *> *))success
                            failure:(void (^)(NSString *))failure {
    
    NSMutableDictionary *mutDic = [model mj_keyValues];
    
    if(![[mutDic objectForKey:@"classifyId"] intValue]) {
        [mutDic removeObjectForKey:@"classifyId"];
    }
    
    HRZAPIRequest *request = [[HRZAPIRequest alloc] initWithAPIRequestType:kAPIRequestType_Get withSubPath:kGoodsListUrl];
    
    request.params = mutDic;
    
    [HRZAPIClient execute:request];
    
    request.ServerSuce = ^(NSDictionary *sucObj) {
        
        NSArray *array = [HRZGoodsClassifyDetailModel mj_objectArrayWithKeyValuesArray:sucObj];
        
        if (success) {
            success(array);
        }
    };
    
    request.ServerFail = ^(NSString *error) {
        if (failure) {
            failure(error);
        }
    };
}

+ (void)getGoodsDetailByGoodsId:(NSString *)goodsId
                        success:(void (^)(HRZGoodsDetailModel *))success
                        failure:(void (^)(NSString *))failure {
    
    NSMutableDictionary *mutDic = [NSMutableDictionary new];
    if (goodsId) [mutDic addEntriesFromDictionary:@{@"goodsId": goodsId}];
    
    HRZAPIRequest *request = [[HRZAPIRequest alloc] initWithAPIRequestType:kAPIRequestType_Get withSubPath:kGoodsDetailUrl];
    
    request.params = mutDic;
    
    [HRZAPIClient execute:request];
    
    request.ServerSuce = ^(NSDictionary *sucObj) {
        NSLog(@"%@", sucObj);
        
        [HRZGoodsDetailModel mj_setupReplacedKeyFromPropertyName:^NSDictionary *{
            return @{
                     @"modelId":@"id",
                     @"detailImgArray":@"detailImg",
                     @"carouselImgArray":@"carouselImg",
                     };
        }];
        
        HRZGoodsDetailModel *model = [HRZGoodsDetailModel mj_objectWithKeyValues:sucObj];
        
        if (success) {
            success(model);
        }
    };
    
    request.ServerFail = ^(NSString *error) {
        if (failure) {
            failure(error);
        }
    };
}

@end
